 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jfinance;

import com.thoughtworks.xstream.XStream;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author efim
 */
public class AccountsModel {
    
    public static final String NEW_ACCOUNT_EVENT = "AccountsModel.NEW_ACCOUNT";
    
    public static final String DELETE_ACCOUNT_EVENT = "AccountsModel.DELETE_ACCOUNT";
    
    public static final String SET_ACCOUNT_EVENT = "AccountsModel.SET_ACCOUNT";
    
    private LinkedList<Account>  accounts;
   
    public AccountsModel(){
        accounts = new LinkedList<>();
    }
    
    public AccountsModel(LinkedList<Account> accounts){
        this.accounts = accounts;        
    }
    
    
    public void addAccount(String name) 
    {
        Account acc = new Account(name);
        accounts.add(acc);   
        firePropertyChange(AccountsModel.NEW_ACCOUNT_EVENT, null, acc);
    }
    public void addAccount(Account account) 
    {        
        accounts.add(account);   
        firePropertyChange(AccountsModel.NEW_ACCOUNT_EVENT, null, account);
    }
    
    public void addAccount(String name,LinkedList<Transaction> transactions) 
    {
        Account acc = new Account(name,transactions);
        accounts.add(acc);
        firePropertyChange(AccountsModel.NEW_ACCOUNT_EVENT, null, acc);
        
    }    
    
    public void setAccountById(int id,Account account) 
    {
        Account old = accounts.get(id);
        accounts.set(id, account);
        firePropertyChange(SET_ACCOUNT_EVENT, old, account);
    }
            
    public void deleteAccountById(int id) 
    {
        Account old = accounts.get(id);
        accounts.remove(id);
        firePropertyChange(DELETE_ACCOUNT_EVENT, old, null);
    }
    
    public void deleteAccount(Account account) 
    {
        accounts.remove(account);
        firePropertyChange(DELETE_ACCOUNT_EVENT, account, null);
    }
    
    public Account getAccountById(int id){
        return accounts.get(id);
    }
    
    public LinkedList<Account> getAccounts(){
        return accounts;
    }

    private final PropertyChangeSupport propertyChangeSupport
            = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(
            PropertyChangeListener listener) {
        propertyChangeSupport.
                addPropertyChangeListener(listener);
    }

    private void firePropertyChange(
            String propertyName, Object oldValue,
            Object newValue) {
        propertyChangeSupport.firePropertyChange(
                propertyName, oldValue, newValue);
    }

    public static class FileIOFactory implements IOFactory{
        
        private String name;
        
        public FileIOFactory(){
            this.name = "data.xml";
        }
        
        public FileIOFactory(String name){
            this.name = name;
        }      
       

        @Override
        public void save(Object o) throws IOException {
            XStream xstream = new XStream();
            String xml = xstream.toXML(o);
            File file = new File(name);
            if(!file.canWrite()) {
               file.createNewFile();
            }
            FileWriter out = new FileWriter(file);
            out.write(xml);
            out.flush();
            out.close();
        }

        @Override
        public Object load() throws IOException {
            XStream xstream = new XStream();
            File file = new File(name);            
            return xstream.fromXML(file);
            
        }
    }
    
    
    
}
